<template>
	<a-spin :spinning="loadSpinning">
		<a-form
			ref="formRef"
			:model="formData"
			:rules="formRules"
			layout="vertical"
			:label-col="{ ...layout.labelCol, offset: 0 }"
			:wrapper-col="{ ...layout.wrapperCol, offset: 0 }"
		>
			<a-form-item label="项目名称：" name="BOTIAN_BLOCKCHAIN_WENCHANG_PROJECT_NAME">
				<a-input v-model:value="formData.BOTIAN_BLOCKCHAIN_WENCHANG_PROJECT_NAME" placeholder="请输入项目名称" />
			</a-form-item>
			<a-form-item label="域名：" name="BOTIAN_BLOCKCHAIN_WENCHANG_DNS">
				<a-input v-model:value="formData.BOTIAN_BLOCKCHAIN_WENCHANG_DNS" placeholder="请输入域名" />
			</a-form-item>
			<a-form-item label="API Key：" name="BOTIAN_BLOCKCHAIN_WENCHANG_APIKEY">
				<a-input v-model:value="formData.BOTIAN_BLOCKCHAIN_WENCHANG_APIKEY" placeholder="请输入API Key" />
			</a-form-item>
			<a-form-item label="API Secret：" name="BOTIAN_BLOCKCHAIN_WENCHANG_APISECRET">
				<a-input v-model:value="formData.BOTIAN_BLOCKCHAIN_WENCHANG_APISECRET" placeholder="请输入API Secret" />
			</a-form-item>
			<a-form-item label="平台链上账户名：" name="BOTIAN_BLOCKCHAIN_WENCHANG_ACCOUNT_NAME">
				<a-input v-model:value="formData.BOTIAN_BLOCKCHAIN_WENCHANG_ACCOUNT_NAME" placeholder="请输入链上账户名" />
			</a-form-item>
			<a-form-item label="平台链上账户地址：" name="BOTIAN_BLOCKCHAIN_WENCHANG_ADDRESS">
				<a-input v-model:value="formData.BOTIAN_BLOCKCHAIN_WENCHANG_ADDRESS" placeholder="请输入链上地址" />
			</a-form-item>
			<a-form-item>
				<a-button type="primary" :loading="submitLoading" @click="onSubmit()">保存</a-button>
				<a-button style="margin-left: 10px" @click="() => formRef.resetFields()">重置</a-button>
			</a-form-item>
		</a-form>
	</a-spin>
</template>

<script setup name="chainForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import { message } from 'ant-design-vue'
	import configApi from '@/api/dev/configApi'

	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	const loadSpinning = ref(true)

	// 查询此界面的配置项,并转为表单
	const param = {
		category: 'BLOCKCHAIN_WENCHANG'
	}
	configApi.configList(param).then((data) => {
		loadSpinning.value = false
		if (data) {
			data.forEach((item) => {
				formData.value[item.configKey] = item.configValue
			})
		} else {
			message.warning('表单项不存在，请初始化数据库')
		}
	})

	// 默认要校验的
	const formRules = {
		BOTIAN_BLOCKCHAIN_WENCHANG_PROJECT_NAME: [required('请输入项目名称')],
		BOTIAN_BLOCKCHAIN_WENCHANG_DNS: [required('请输入域名')],
		BOTIAN_BLOCKCHAIN_WENCHANG_APIKEY: [required('请输入API Key')],
		BOTIAN_BLOCKCHAIN_WENCHANG_APISECRET: [required('请输入API Secret')],
		BOTIAN_BLOCKCHAIN_WENCHANG_ACCOUNT_NAME: [required('请输入链上账户名')],
		BOTIAN_BLOCKCHAIN_WENCHANG_ADDRESS: [required('请输入链上地址')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			let submitParam = cloneDeep(formData.value)
			const param = Object.entries(submitParam).map((item) => {
				return {
					configKey: item[0],
					configValue: item[1]
				}
			})
			configApi
				.configEditForm(param)
				.then(() => {})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	const layout = {
		labelCol: {
			span: 4
		},
		wrapperCol: {
			span: 12
		}
	}
</script>
